package j06tongbu;

public class j1 {
    static int count = 100;
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while (count>0){
                System.out.println("t1卖出="+count);
                count--;
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread t2 = new Thread(()->{
            while (count>0){
                System.out.println("t2卖出="+count);
                count--;
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
}

class J2 {
    static Object lock = new Object();
    static int count = 100;
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while (count>0){
                //t1 lock上锁
                synchronized (J2.class){}
                synchronized (lock){
                    //100
                    System.out.println("t1卖出="+count);
                    count--;
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            }
        });
        Thread t2 = new Thread(()->{
            while (count>0){
                synchronized (lock){
                    System.out.println("t2卖出="+count);
                    count--;
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            }
        });
        t1.start();
        t2.start();

        //class也是对象
        System.out.println(J2.class);
    }
}
